package com.wanda.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TimerTask;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.mysql.jdbc.Driver;
import com.wanda.controller.OrderController;
import com.wanda.controller.Wd_UserController;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.Wd_UserService;
@Controller
public class NFDFlightDataTimerTask extends TimerTask  {

	private  String projectManagerRoleId = "21";
	
	
    private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void run() {
          try {
        	ContractJDBC cjdbc = new ContractJDBC();
        	
        	String sql = "select email from wd_user where 1=1 and rol_id='"+projectManagerRoleId+"' and  organization_id in(select organization_id from wd_user GROUP BY organization_id)";
        	List<String> listw = cjdbc.getEmail(sql);
        	System.out.println("wd_UserService"+listw);
        	//List<Wd_UserModel> userList = user.selectBySql(sql);
        
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Map<String,String> map= new HashMap<String,String>();
            MailUtil mail = new MailUtil("937590316@qq.com","ntzcmjgpcfzebajj");
            LoadEmail le = new LoadEmail();
            Properties properties = le.loalMailproperties();
            Set<Object> set = properties.keySet();
            for(Object o:set){
            	 map.put(o.toString(), properties.getProperty(o.toString())); 
            }
            for(String s:listw){
              mail.setPros(map);
              mail.initMessage();
        	  mail.setRecipients(s);
              mail.setSubject(sdf.format(Calendar.getInstance().getTime())+"签约报告");
              mail.setDate(new Date());
              mail.setFrom("万达地产");//给对应的组中找到对应的user找到对应的订单
              mail.setContent(cjdbc.getSendManager(s), "text/html; charset=UTF-8");
              System.out.println(mail.sendMessage());
              System.out.println("执行当前时间"+formatter.format(Calendar.getInstance().getTime()));
            }

        	} catch (Exception e) {
           e.printStackTrace();
        }
    }
}
class DBHelper {  
	    public Connection getConnection(){
	    	String driverName = "com.mysql.jdbc.Driver";  
	    	String driverPath = "jdbc:mysql://127.0.0.1/wanda_java2";  
	    	String name = "root";  
	    	String pwd = "root";  
	        Connection connection = null;
	        try {
	            //1.通过反射机制获取Driver实现类的对象
	            @SuppressWarnings("static-access")
	            Driver driver = (Driver)getClass().forName(driverName).newInstance();
	            Properties info = new Properties();
	            info.put("user", name);
	            info.put("password", pwd);
	            //2.获取数据库连接对象
	            connection = driver.connect(driverPath, info);
	        } catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        return connection;
	    }
	  
	    public void close(Connection conn) {  
	        try {  
	            conn.close();  
	        } catch (SQLException e) {  
	            e.printStackTrace();  
	        }  
	    }  


	public List<String> selectEmail(String sql) {
		List<String >list=null;
		Connection coon = getConnection();
	        try {
	        	PreparedStatement pStatement = coon.prepareStatement(sql);
	            ResultSet resultSet = pStatement.executeQuery();
	            while (resultSet.next()) {
	               String email = resultSet.getString("email");
	             list.add(email);
	            }
	        } catch (SQLException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        return list;
	    }
 
}